<?php
class Site
{
	/**
	 * Datenbankverbindung
	 *
	 * @var Kanedo_Db
	 */
	protected $_db = null;
	
	/**
	 * Tabellenname
	 *
	 * @var string
	 */
	protected $_tableSite = "site";
	/**
	 * Der Primaerschluessel der Tabelle
	 *
	 * @var string
	 */
	protected $_tablePrimaryKey	= "id_site";
	/**
	 * Die Titelspalte
	 *
	 * @var string
	 */
	protected $_tableTitle = "title";
	/**
	 * Die Inhaltsspalte
	 *
	 * @var string
	 */
	protected $_tableContent = "content";
	/**
	 * Die Datumsspalte
	 *
	 * @var string
	 */
	protected $_tableDate	= "date";
	/**
	 * Spalte ob Oeffentlich oder nicht
	 *
	 * @var string
	 */
	protected $_tablePublished = "published";
	/**
	 * BenutzerIDspalte
	 *
	 * @var string
	 */
	protected $_tableUserId = "user_user_id_user_user";
	
	
	/**
	 * Konstrukt
	 *
	 * @param Kanedo_Db $db
	 */
	public function __construct(Kanedo_Db $db)
	{
		$this->_db = $db;
		if (!$this->_db->isConnected())
		{
			throw new Kanedo_Db_Exception("No connection to DB found!");
		}
	}
	
	/**
	 * Konfiguriert die Klasse um, nur benoetigte Veraenderungen werden angegeben
	 *
	 * @param array $config
	 */
	public function configure ($config)
	{
		if(key_exists($config, "table"))
		$this->_tableSite = $config['table'];
		
		if(key_exists($config, "primarykey"))
		$this->_tablePrimaryKey = $config['primarykey'];
		
		if(key_exists($config, "title"))
		$this->_tableTitle = $config['title'];
		
		if(key_exists($config, "content"))
		$this->_tableContent = $config['content'];
		
		if(key_exists($config, "date"))
		$this->_tableDate = $config['date'];
		
		if(key_exists($config, "published"))
		$this->_tablePublished = $config['published'];
		
		if(key_exists($config, "userid"))
		$this->_tableUserId = $config['userid'];
	}
	

	/**
	 * Holt eine Site mithile einer ID
	 *
	 * @param int $id
	 * @return array
	 */
	public function getSiteById($id)
	{
		$sql = "SELECT * FROM `".$this->_tableSite."` WHERE `".$this->_tablePrimaryKey."` = ".$id;
		$result = $this->_db->executeQuery($sql);
		$return = $this->_db->fetchAll($result);
		if (empty($return))
		{
			throw new Kanedo_Db_Exception("No entry for this id!", 404);
		}
		return $return;
	}
	
	
	/**
	 * Funktion zum Einfuegen einer Seite, gibt ihre ID zurueck
	 *
	 * @param string $title
	 * @param string $content
	 * @param string $date
	 * @param int $user
	 * @param int $published
	 * @return int
	 */
	public function insertSite( $title,  $content,  $date,  $user,  $published = 1)
	{
		$sql = "INSERT INTO `".$this->_tableSite."` (".$this->_tableTitle.", ".$this->_tableContent.",".$this->_tableDate.",".$this->_tableUserId.",".$this->_tablePublished.") 
				VALUES ('".$title."', '".$content."', '".$date."', ".$user.", ".$published.")";
		$result = $this->_db->executeQuery($sql);
		if(!$result)
		{
			require_once "Kanedo/Db/Exception.php";
			throw new Kanedo_Db_Exception("invalid_ressource{".mysql_error()."}");
		}
		return mysql_insert_id();
	}
	
	public function updateSite( $id, $title,  $content,  $date,  $user,  $published = 1)
	{
		$sql = "UPDATE`".$this->_tableSite."` SET 	".$this->_tableTitle." = '".$title."',
													".$this->_tableContent." = '".$content."',
													".$this->_tableDate." = '".$date."',
													".$this->_tableUserId." = ".$user.",
													".$this->_tablePublished." = ".$published." WHERE ".$this->_tablePrimaryKey." =".$id;
		$this->_db->executeQuery($sql);
		
	}
}
?>